package com.wanxi.dao.Impl;

import com.wanxi.dao.ServiceTypeDao;
import com.wanxi.model.ServiceTypeModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Li
 * @Date 2023/5/7 19:32
 */
public class ServiceTypeDaoImpl implements ServiceTypeDao {
    public int add(ServiceTypeModel serviceTypeModel) {
        String sql = "insert into service_area (`service_type`,`create_time`,`update_time`,`enable`,`remark`) " +
                "value('"+serviceTypeModel.getServiceType()+"',now(),sysdate(),'"+serviceTypeModel.getEnable()+"','"+serviceTypeModel.getRemark()+"');";
        return JDBC.update(sql);
    }

    public int update(ServiceTypeModel serviceTypeModel) {
        String sql = "update service_area set `service_type`='"+serviceTypeModel.getServiceType()+"',`update_time`=now()," +
                " `enable`='"+serviceTypeModel.getEnable()+"',`remark`='"+serviceTypeModel.getRemark()+"' where (`id` = '"+serviceTypeModel.getId()+"') ";
        return JDBC.update(sql);
    }

    public int delete(ServiceTypeModel serviceTypeModel) {
        String sql = "delete from service_area where id='"+serviceTypeModel.getId()+"' ";
        return JDBC.update(sql);
    }

    public List<ServiceTypeModel> findAll(ServiceTypeModel serviceTypeModel) {
        int start = (serviceTypeModel.getPage()-1)*serviceTypeModel.getLimit();
        String sql = "select * from service_area where true";
         sql += getWhereSql(serviceTypeModel);
        sql += " order by id desc";
        sql += " limit "+start+","+serviceTypeModel.getLimit()+" ";
        ResultSet query = JDBC.query(sql);
        List<ServiceTypeModel> serviceTypeModelList = new ArrayList<ServiceTypeModel>();
        try {
            while (query.next()){
                serviceTypeModelList.add(ServiceTypeModel.builder()
                        .id(query.getInt("id"))
                        .serviceType(query.getString("service_type"))
                        .enable(query.getInt("enable"))
                        .remark(query.getString("remark"))
                        .createTime(query.getString("create_time"))
                        .build());
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return serviceTypeModelList;
    }
private String getWhereSql(ServiceTypeModel serviceTypeModel){
        String sql = "";
    if (!"".equals(serviceTypeModel.getServiceType())){
        sql += " and service_type like '%"+serviceTypeModel.getServiceType()+"%' ";
    }if (serviceTypeModel.getEnable()!=-1){
        sql +=" and enable = '"+serviceTypeModel.getEnable()+"' ";
    }
    return sql;
}
    /**
     * 根据id查询
     * @param serviceTypeModel
     * @return
     */
    public ServiceTypeModel findById(ServiceTypeModel serviceTypeModel) {
        String sql = " select * from `service_area` where id = '"+serviceTypeModel.getId()+"' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
            return ServiceTypeModel.builder()
                    .id(query.getInt("id"))
                    .serviceType(query.getString("service_type"))
                    .enable(query.getInt("enable"))
                    .remark(query.getString("remark"))
                    .build();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public Integer count(ServiceTypeModel serviceTypeModel) {
        String sql = "select count(*) as count from `service_area` where true";
       sql += getWhereSql(serviceTypeModel);
        return JDBC.count(sql);
    }

    @Override
    public Integer totalCount() {
        return null;
    }

    public boolean findSameName(ServiceTypeModel serviceTypeModel) {
        String sql = "select * from service_area where service_type = '"+serviceTypeModel.getServiceType()+"' ";
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
